package com.yunshang.shoppingmall.controller.User;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunshang.shoppingmall.common.MessageConstants;
import com.yunshang.shoppingmall.common.Response;
import com.yunshang.shoppingmall.common.layuiJson;
import com.yunshang.shoppingmall.entity.UserRank;
import com.yunshang.shoppingmall.service.User.UserRankService;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserRankController {

    @Autowired
    UserRankService userRankService;

    //会员等级列表页面
    @RequestMapping("/user/getUserRankList")
    public String getUserRankList() {
        return "user/UserRankList";
    }

    //会员等级列表Json数据
    @ResponseBody
    @RequestMapping("/user/JsonUserRankList")
    public String JsonUserRankList(int page,int limit) {
        PageHelper.startPage(page,limit);
        List<UserRank> userRankList = userRankService.selectUserRankList();
        PageInfo page1 = new PageInfo(userRankList);
        return layuiJson.layuiJsonList(userRankList,(int)page1.getTotal());
    }

    //会员等级列表页面
    @RequestMapping("/user/getUserRankAdd")
    public String getUserRankAdd() {
        return "user/UserRankAdd";
    }

    //增加会员等级
    @ResponseBody
    @RequestMapping(value = "/user/addUserRank")
    @Transactional
    public Map<String, Object> addUserRank(@RequestParam("rankname") String rankname,
                                           @RequestParam("recommend") double recommend,
                                           @RequestParam("commission") double commission,
                                           @RequestParam("sort") int sort) {
        Logger logger = LoggerFactory.getLogger(UserRankController.class);
        System.out.println("---->" + rankname);
        Map<String, Object> map = new HashMap<>();
        if (rankname != "") {
            UserRank userRank = new UserRank();
            userRank.setRankname(rankname);
            userRank.setRecommend(recommend);
            userRank.setCommission(commission);
            userRank.setSort(sort);
            logger.info(userRank.toString());
            int rows = userRankService.insert(userRank);
            if (rows > 0) {
                map.put("result", rows);
                return map;
            } else {
                map.put("result", "添加失败！");
                return map;
            }

        } else {
            map.put("result", "添加的值不能为空！");
            return map;
        }
    }

    //根据等级ID查询会员等级信息
    @RequestMapping("/user/getUserRankEdit")
    public ModelAndView getUserRankEdit(Integer rankid) {
        ModelAndView modelAndView = new ModelAndView("user/UserRankEdit");
        //查询列表
        int adId = Integer.valueOf(rankid);
        UserRank userRank = userRankService.selectByPrimaryKey(adId);

        modelAndView.addObject(userRank);
        return modelAndView;
    }

    //根据等级ID查询会员等级信息
    @RequestMapping("/user/getUserRankShow")
    public ModelAndView getUserRankShow(Integer rankid) {
        ModelAndView modelAndView = new ModelAndView("user/UserRankShow");
        //查询列表
        int adId = Integer.valueOf(rankid);
        UserRank userRank = userRankService.selectByPrimaryKey(adId);

        modelAndView.addObject(userRank);
        return modelAndView;
    }

    //编辑会员等级
    @ResponseBody
    @RequestMapping(value = "/user/editUserRank")
    @Transactional
    public Map<String, Object> editUserRank(@RequestParam("rankname") String rankname,
                                           @RequestParam("rankid") int rankid,
                                           @RequestParam("recommend") double recommend,
                                           @RequestParam("commission") double commission,
                                           @RequestParam("sort") int sort) {
        Logger logger = LoggerFactory.getLogger(UserRankController.class);
        System.out.println("---->" + rankname);
        Map<String, Object> map = new HashMap<>();
        if (rankname != "") {
            UserRank userRank = new UserRank();
            userRank.setRankid(rankid);
            userRank.setRankname(rankname);
            userRank.setRecommend(recommend);
            userRank.setCommission(commission);
            userRank.setSort(sort);
            logger.info(userRank.toString());
            int rows= userRankService.updateByPrimaryKeySelective(userRank);
            if (rows > 0) {
                map.put("result", rows);
                return map;
            } else {
                map.put("result", "更新失败！");
                return map;
            }

        } else {
            map.put("result", "更新不能为空！");
            return map;
        }
    }

    //根据rankId删除会员等级
    @ResponseBody
    @RequestMapping("/user/delUserRank")
    @Transactional
    public String delUserRank(int rankId){

        int rankIds =Integer.valueOf(rankId);
        int rows= userRankService.deleteByPrimaryKey(rankIds);
        return "{result:"+rows+"}";
    }
}